package com.qyer.util.log.util;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.ImmutableMap;

public class UgcTypeUtil {
	
	/**
	 * 获取城市id
	 * 
	 * @return
	 */
	public static String getCityID(String cityName ,Map<String ,String >cityMap ) {
		String cityID = "-";
		if (null != cityMap) {
			if (cityMap.size() > 0) {
				String temp = cityMap.get(cityName);
				if (null != temp) {
					cityID = temp;
				}
			}
		}
		return cityID;
	}

	/**
	 * 获取国家id
	 * 
	 * @return
	 */
	public static String getCountryID(String countryName ,Map<String ,String >countryMap) {
		String countryID = "-";
		if (null != countryMap) {
			if (countryMap.size() > 0) {
				String temp = countryMap.get(countryName);
				if (null != temp) {
					countryID = temp;
				}
			}
		}
		return countryID;
	}
	
	
	/**
	 * 获取国家id
	 * 
	 * @return
	 */
	public static String getJN_ID(String jn_name ,Map<String ,String > jn_info_map) {
		String jn_ID = "-";
		if (null != jn_info_map) {
			String temp = jn_info_map.get(jn_name);
			if (null != temp) {
				jn_ID = temp;
			}
		}
		return jn_ID;
	}
	
	public static final Map<String,String> continentMap = ImmutableMap.<String,String>builder()
			.put("asia","10")
			.put("europe","12")
			.put("africa","76")
			.put("north-america","234")
			.put("south-america","235")
			.put("oceania","239")
			.put("antarctica","759")
			.build();
	
	public static String getUgcType(String url ,List<String >listUGC , Map<String ,String >cityMap , Map<String ,String >countryMap, Map<String, String> jn_info_Map, Map<String, String>  sw_map){
		String result="-,-";
		String ugc_type= "-";
		String ugc_id=  "-";
		
		if (null != url && !url.equals("")) {
			if (listUGC.size() > 0) {
				for (String ugc_rule_array : listUGC) {
					if (null != ugc_rule_array
							&& !ugc_rule_array.equals("")) {
						String[] ugc_array = ugc_rule_array.split("##");
						if (ugc_array.length > 2) {
							String ugc_rule = ugc_array[0];
							String ugc_type_str = ugc_array[1];
							String ugc_id_Str = ugc_array[2];
							if (getRegExBool(ugc_rule, url)) {// 如果正则匹配
								String temp = getRegEx(ugc_rule, url);// 获取取出的内容
								if (ugc_type_str.equals("countryorcity")) {// 如果是匹配的是国家或城市的
									String countryID = getCountryID(temp,countryMap);
									if (!countryID.equals("-")) {
										ugc_type = "place_country";
										ugc_id = countryID;
									} else {
										String cityID = getCityID(temp,cityMap);
										if (!cityID.equals("-")) {
											ugc_type = "place_city";
											ugc_id = cityID;
										}else if(continentMap.containsKey(temp)){
											ugc_type = "place_continent";
											ugc_id = continentMap.get(temp);
										}else {
											ugc_type = "-";
											ugc_id = "-";
										}
									}
								} else if (ugc_type_str
										.equals("mcountryorcity")) {// 如果是匹配的是国家或城市的
									String countryID = getCountryID(temp,countryMap);
									if (!countryID.equals("-")) {
										ugc_type = "mplace_country";
										ugc_id = countryID;
									} else {
										String cityID = getCityID(temp,cityMap);
										if (!cityID.equals("-")) {
											ugc_type = "mplace_city";
											ugc_id = cityID;
										} else if(continentMap.containsKey(temp)){
											ugc_type = "mplace_continent";
											ugc_id = continentMap.get(temp);
										}else {
											ugc_type = "-";
											ugc_id = "-";
										}
									}
								}else if (ugc_type_str.equals("countryorcitytravel")) {// 如果是匹配的是国家或城市的
									String countryID = getCountryID(temp,countryMap);
									if (!countryID.equals("-")) {
										ugc_type = "place_country_alltravel";
										ugc_id = countryID;
									} else {
										String cityID = getCityID(temp,cityMap);
										if (!cityID.equals("-")) {
											ugc_type = "place_city_alltravel";
											ugc_id = cityID;
										} else {
											ugc_type = "-";
											ugc_id = "-";
										}
									}
								} else if (ugc_type_str
										.equals("mcountryorcitytravel")) {// 如果是匹配的是国家或城市的
									String countryID = getCountryID(temp,countryMap);
									if (!countryID.equals("-")) {
										ugc_type = "mplace_country_alltravel";
										ugc_id = countryID;
									} else {
										String cityID = getCityID(temp,cityMap);
										if (!cityID.equals("-")) {
											ugc_type = "mplace_city_alltravel";
											ugc_id = cityID;
										} else {
											ugc_type = "-";
											ugc_id = "-";
										}
									}
								} else if (ugc_type_str
										.equals("hotel_city")) {// 酒店城市
									String cityID = getCityID(temp,cityMap);
									if (!cityID.equals("-")) {
										ugc_type = "hotel_city";
										ugc_id = cityID;
									} else {
										ugc_type = "-";
										ugc_id = "-";
									}
								} else if (ugc_type_str
										.equals("hotel_country")) {// 酒店国家
									String countryID =getCountryID(temp,countryMap);
									if (!countryID.equals("-")) {
										ugc_type = "hotel_country";
										ugc_id = countryID;
									} else {
										ugc_type = "-";
										ugc_id = "-";
									}
								}else if (ugc_type_str.equals("guide_detail") || ugc_type_str.equals("mguide_detail")) {//锦囊详情页
									String jn_id = getJN_ID(temp,jn_info_Map);
									ugc_type = ugc_type_str;
									if("-".equals(jn_id)){
										ugc_type="-";
									}
									ugc_id = jn_id;
								} else {
									ugc_type = ugc_type_str;
									if (ugc_id_Str.equals("-")) {// 判断是否为不需要获取具体的内容
										ugc_id = "-";
									} else {
										ugc_id = temp;
									}
									if (ugc_type_str.indexOf("search")!=-1) {// 如果为搜索页，则需要转码
										try {
											ugc_id = URLDecoder.decode(ugc_id);
										} catch (Exception e) {
										}
										if(StringUtils.isNumeric(ugc_id) ){//数字转搜索词
											String word=sw_map.get(ugc_id);
											if(word != null){
												ugc_id=word;
											}
										}
									}
								}

								return ugc_type + "," + ugc_id;
							}
						}
					}
				}

			} else {
				System.out.println("没有正则信息");
				return result;
			}

	}
	return result;
	}
	
	/**
	 * 正则匹配获取匹配的内容
	 * 
	 * @param regex
	 * @param url
	 * @return
	 */
	public static boolean getRegExBool(String regex, String url) {
		boolean result = false;
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(url);
		while (mat.find()) {
			result = true;
		}
		return result;
	}

	/**
	 * 正则匹配获取匹配的内容
	 * 
	 * @param regex
	 * @param url
	 * @return
	 */
	public static String getRegEx(String regex, String url) {
		String result = "-,-";
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(url);
		while (mat.find()) {
			if (mat.groupCount() > 0) {
				if (null != mat.group(1)) {
					result = mat.group(1);
				}
			}

		}
		return result;
	}
}
